﻿using System;
using System.Data;
using System.Data.SqlServerCe;
using System.Reflection;
using MWTools.Client.Common;
using MWTools.Client.Common.Interfaces;

namespace MWTools.Client.Dal
{
    public class UserSettingDal
    {
        #region UserSetting CRUD

        public static DataTable UserSettingSelect(string userName)
        {
            DataTable dataTable = new DataTable();

            using (SqlCeConnection connection = new SqlCeConnection(string.Format("Data Source={0}", Constants.SETTINGS_STORE_PATH)))
            {
                string commandText = "SELECT * FROM UserSetting ";

                if (userName != null)
                    commandText += string.Format(" WHERE UserName = '{0}'", userName);

                using (SqlCeCommand command = new SqlCeCommand(commandText, connection))
                {
                    connection.Open();
                    using (SqlCeDataAdapter sda = new SqlCeDataAdapter(command))
                    {
                        sda.Fill(dataTable);
                    }
                }
            }


            return dataTable;
        }
        public static void UserSettingInsert(UserSetting setting)
        {
            using (SqlCeConnection connection = new SqlCeConnection(string.Format("Data Source={0}", Constants.SETTINGS_STORE_PATH)))
            {
                string commandText = string.Format(
                                         "INSERT INTO [UserSetting]( "
      + "[UserName]"
      + "   ,[Password] "
         + "   ,[DataLastRefreshedOn] "
       + "     ,[MWToolsServerUri] "
        + "    ,[LogFilePath], CurrentPlugin, PluginAssembly) "
   + "   VALUES "
          + "( '{0}',  "
          + " '{1}',  "
          + " '{2}',  "
          + " '{3}',  "
          + " '{4}', '{5}', '{6}' );",
                                         setting.UserName,
                                         setting.Password,
                                         DateTime.Now,
                                         setting.MWToolsServerUri,
                                         setting.LogFilePath,
                                         setting.CurrentPlugin,
                                         setting.PluginAssembly);
                using (SqlCeCommand command = new SqlCeCommand(commandText, connection))
                {
                    connection.Open();
                    try
                    {
                        Logger.DebugFormat("UserSettingInsert", "{0}", command.CommandText);

                        command.CommandType = CommandType.Text;
                        command.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        Logger.FatalFormat("UserSettingDal.UserSettingInsert", "{0}", ex);
                        throw;
                    }
                }
            }
        }

        public static void UserSettingDelete(string userName)
        {
            using (SqlCeConnection connection = new SqlCeConnection(string.Format("Data Source={0}", Constants.SETTINGS_STORE_PATH)))
            {
                string commandText = string.Format("DELETE FROM UserSetting", userName);

                using (SqlCeCommand command = new SqlCeCommand(commandText, connection))
                {
                    connection.Open();
                    Logger.DebugFormat(MethodBase.GetCurrentMethod().DeclaringType, "executing sql{0}", commandText);
                    try
                    {
                        command.CommandType = CommandType.Text;
                        command.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        Logger.FatalFormat("UserSettingDal.UserSettingDelete", "{0}", ex);

                        throw;
                    }

                }
            }

        }
        #endregion
    }

}
